package com.hjxr.quota.sdoc.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.quota.sdoc.service.SdocColnameService;
import com.hjxr.quota.util.FileUtils;
import com.hjxr.quota.sdoc.service.FileOperationService;
import com.hjxr.quota.sdoc.service.SdocSevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author ma
 * @date 2020/9/4 - 10:43
 **/
@RestController
@RequestMapping("/filOpe")
@Api(tags = "文件处理接口")
public class FileOperationController extends ApiController {

    @Value("${hjxr.profile}")
    private String proFilePath;
    @Autowired
    private FileOperationService fileOperationService;
    @Autowired
    private SdocSevice sdocSevice;
    @Autowired
    private SdocColnameService sdocColnameService;

    @PostMapping("/upload")
    @ApiOperation("上传文件并将文件信息存入文件信息表")
    private R<String> upload(@RequestParam("filePath") MultipartFile file, HttpServletRequest request) throws Exception {

        R<String> success = success(fileOperationService.fileUpload(file));
        if (success.getCode() == 0) {
            Integer num = sdocSevice.fileInformationToDb(file.getOriginalFilename(), success.getData(), request);//插入返回id 失败为0
            if (num != 0 && sdocColnameService.fieldsInformationToDb(proFilePath, success.getData(), num, request)) {
                return success;
            }
            return success.setMsg("文件上传成功；文件信息存入表失败！");
        }
        return success;
    }

    @GetMapping("/getfilehead")
    @ApiOperation("获取文件头")
    private R<List<String>> getFileHead(@RequestBody String fileName) throws Exception {
        return success(FileUtils.readFileHead(proFilePath, fileName));
    }

    @PostMapping("/fileToDb/{id}")
    @ApiOperation("文件存储到数据表")
    private R<Boolean> fileToDb(@PathVariable("id") Integer id) throws Exception {
        return success(fileOperationService.fileToDb(id));
    }

    @PostMapping("/checkTableExist")
    @ApiOperation("检查表是否存在")
    private R<Integer> checkTableExist(@RequestBody String tableName) {
        return success(fileOperationService.checkTableExist(tableName));
    }


    /**
     * @param id
     */
    @GetMapping("/checkDataLength/{id}")
    @ApiOperation("数据校验")
    private R<Map<String, Object>> checkDataLength(@PathVariable("id") Integer id) {
        return success(fileOperationService.checkDataLength(id));
    }

    /**
     * @param id 标准文件id
     * @return 0失败1成功
     */
    @GetMapping("/createtable/{id}")
    @ApiOperation("根据文件信息建相关数据表")
    private R<Integer> createTableByFileInform(@PathVariable("id") Integer id) {
        return success(fileOperationService.createTableByFileInform(id));
    }


}
